﻿using System;
using System.Collections;

namespace popLib
{

	public static class NumberLib
	{
		
		static string hexa = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E";
		static string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
				
	
		//-------------------Convert function-----------------
	    
		/// <summary>
		/// get Decimal (A B -> 65 32 66)
		/// var is a input string, midstr is a string between two decimals
		/// (midstr = " " -->A B -> 65 32 66
		/// midstr = " 00 " -->A B -> 65 00 32 00 66 00)
		/// </summary>
		public static  string getDecimal(string var,string midstr)//get Decimal (A B -> 65 32 66)
		{
			/// <summary>
			/// Lib1 include some functions about string, hexadecimal, decimal, binary...
			/// </summary>
			string rs = "";
			string sp = midstr;
	
			for(int i=0;i<var.Length;i++)
			{
				if(text.IndexOf(var.Substring(i,1)) >=0 )
					rs += (text.IndexOf(var.Substring(i,1)) + 32) + sp ;
			}
			return rs;
		}
	
		
		public static  string deDecimal(string var,bool addOutChar)//get text from decimal (65 32 66 --> A B )
		{
			string	rs  = "";
			int		buf = 0;			
			
			if(var.Replace(" ","").Length > 0)
			{
				var = StringLib.filterStr(var," 0123456789");
			
				if(var.IndexOf(" ") >= 0)
				{
					string[] arr = var.Split(' ');
					for(int i=0;i<arr.Length;i++)
					{
						if(arr[i] == "") buf = -1;
						else	buf = Convert.ToInt32(arr[i]);
	
						if( buf >= 32 && buf-32 <= 94)
							rs += text.Substring( buf - 32 , 1 ) + "";
						else if(addOutChar && buf >=0 )
							rs += "[" + buf + "]" ;											
					}
				}
				else if( getInt(var) >= 32 && getInt(var)-32 <= 94)
					rs = text.Substring(getInt(var) - 32,1);
				else if(addOutChar)
					rs = "[" + getInt(var) + "]" ;
			}
	
			return rs;
		}
		//End
	
	
		//Hexadecimal
		public static  string getHexa(string var,bool addMidStr)//get hexadecimal (LM -> 4B 4D)
		{
			string rs = "";
			string sp = "";
			for(int i=0;i<var.Length;i++)
			{
				if(i==var.Length-1)
					sp = "";
				else
					sp = " ";
	
				if(addMidStr)
					rs += hexa.Substring( (text.IndexOf(var.Substring(i,1) ) * 2), 2 )+" 00" + sp;
				else
					rs += hexa.Substring( (text.IndexOf(var.Substring(i,1) ) * 2), 2 )+sp;					
			}
			
			return rs;
		}
	
		public static  string deHexa(string var,bool addOutHex)//get text from hexadecimal (4B 4D -> LM)
		{
			string rs  = "";
			string buf = "";
			bool   foundit = false;
			var = var.Replace(" ","");
			
			if( var.Length%2 == 1)
				var = var.Substring(0,var.Length-1);
	
			for(int i=0;i<var.Length;i+=2)
			{
				buf = var.Substring(i,2).ToUpper();
				for(int j=0;j<95;j++)
				{
					if( hexa.Substring(j*2,2).Equals(buf))
					{
						rs += text.Substring(j,1);
						foundit = true;
						break;
					}
					else 
						foundit = false;
				}		
					if(addOutHex && !foundit)
						rs += "["+ buf +"]";
				
			}
			return rs;
		}
		//end
	
	
		//Binary
		public static  string getBinary(string var1)//get binary (12 -> 1100)
		{
			int var = getInt(var1);
			string rs = "";
			while( var !=0 )
			{
				rs += var % 2 ;
				var = var/2;
			}
			string rs1 = "";
			for(int i=rs.Length-1;i>-1;i--)
				rs1+=rs.Substring(i,1);			
			return rs1;
		}
	
		public static  string deBinary(string var)//get text from binary (1100 -> 12)
		{	
			var = StringLib.filterStr(var,"01");			
			int rs = 0;
			for(int i=0;i<var.Length;i++)
				rs = (rs + Convert.ToInt16(var.Substring(i,1)) )*2;
			
			return rs/2 + "";
		}
		//End
	
		//----------------End Convert function-----------------

        public static char DecimalSymbol { get { return (1.5).ToString("#,0.0").Replace("1", "").Replace("5", "")[0]; } }
        public static char DigitGroupSymbol { get { return (1000).ToString("#,0").Replace("1", "").Replace("0", "")[0]; } }

		//Return number only
		public static int getInt(object obj)
		{				
			if( obj == null ) return 0;
            return Convert.ToInt32(fixNumber(obj.ToString()).Split(DecimalSymbol)[0]);
		}
		
		//Return number only
		public static long getInt64(object obj)
		{				
			if( obj == null ) return 0;
            return Convert.ToInt64(fixNumber(obj.ToString()).Split(DecimalSymbol)[0]);
		}
		
		//Return number only
		public static double getDouble(object obj)
		{
			if( obj == null ) return 0;
			return Convert.ToDouble( fixNumber( obj.ToString() ) );
		}
				
		public static double getDouble( object obj, int decimalLength )
        {
			if( obj == null ) return 0;
			string number = fixNumber( obj.ToString() );
            int dotIndexer = number.IndexOf(DecimalSymbol);
			
			if( dotIndexer >= 0 && number.Length - dotIndexer > decimalLength+1 )
				number = number.Substring( 0, dotIndexer+decimalLength+1 );
			return Convert.ToDouble( number );
		}
		
		public static string fixNumber( object obj )
        {
            string number = StringLib.filterStr(obj.ToString(), "-0123456789" + DecimalSymbol);
			
			if(number.Contains( "-" )){
				if( number.StartsWith("-") ) number = "=" + number;
				number = number.Replace("-","").Replace("=","-");
			}

            if (number.Contains(DecimalSymbol+""))
            {
                if (number.StartsWith(DecimalSymbol+"")) number = "0" + number;
                if (number.EndsWith(DecimalSymbol+"")) number = number.Substring(0, number.Length - 1);
                number = StringLib.Replace1time(number, DecimalSymbol + "", "*").Replace(DecimalSymbol + "", "").Replace("*", DecimalSymbol+"");
			}
			
			if( (number.IndexOf("-") == 0 && number.Length == 1) || number == "-0" || number.Length == 0 ) return "0";			
			
			return number;
		}
		
				
		public static DateTimeLib.CompareType compareNumber(string _num1, string _num2){
			double num1 = getDouble(_num1);
			double num2 = getDouble(_num2);
			
			if( num1 > num2 ) return DateTimeLib.CompareType.higher;
			if( num1 < num2 ) return DateTimeLib.CompareType.lower;
			return DateTimeLib.CompareType.equal;
		}

        public static string FormatMoney(this double money, int nfloat)
        {			
            string fpoint = StringLib.spaceleft("", nfloat, "0");
            if (fpoint.Length > 0) fpoint = "." + fpoint;

            return money.ToString("#,0" + fpoint);
		}

        public static string FormatMoney(this double money)
        {
            return FormatMoney(money, 0);
		}

        public static string MoneyToTextString(this double number)
        {
            string s = number.ToString("#");
            string[] so = new string[] { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
            string[] hang = new string[] { "", "nghìn", "triệu", "tỷ" };
            int i, j, donvi, chuc, tram;
            string str = " ";
            bool booAm = false;
            double decS = 0;
            //Tung addnew
            try
            {
                decS = Convert.ToDouble(s.ToString());
            }
            catch
            {
            }
            if (decS < 0)
            {
                decS = -decS;
                s = decS.ToString();
                booAm = true;
            }
            i = s.Length;
            if (i == 0)
                str = so[0] + str;
            else
            {
                j = 0;
                while (i > 0)
                {
                    donvi = Convert.ToInt32(s.Substring(i - 1, 1));
                    i--;
                    if (i > 0)
                        chuc = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        chuc = -1;
                    i--;
                    if (i > 0)
                        tram = Convert.ToInt32(s.Substring(i - 1, 1));
                    else
                        tram = -1;
                    i--;
                    if ((donvi > 0) || (chuc > 0) || (tram > 0) || (j == 3))
                        str = hang[j] + str;
                    j++;
                    if (j > 3) j = 1;
                    if ((donvi == 1) && (chuc > 1))
                        str = "một " + str;
                    else
                    {
                        if ((donvi == 5) && (chuc > 0))
                            str = "lăm " + str;
                        else if (donvi > 0)
                            str = so[donvi] + " " + str;
                    }
                    if (chuc < 0)
                        break;
                    else
                    {
                        if ((chuc == 0) && (donvi > 0)) str = "lẻ " + str;
                        if (chuc == 1) str = "mười " + str;
                        if (chuc > 1) str = so[chuc] + " mươi " + str;
                    }
                    if (tram < 0) break;
                    else
                    {
                        if ((tram > 0) || (chuc > 0) || (donvi > 0)) str = so[tram] + " trăm " + str;
                    }
                    str = " " + str;
                }
            }
            if (booAm) str = "Âm " + str;

            str = str + "đồng chẵn";

            str = StringLib.UpperFirstLetter(str.Trim().Replace("  ", " "));

            return str;
        }


	}
}

